#include "serial_client.h"
#include <QDebug>
SerialClient::SerialClient(QObject *parent) : QObject(parent)
{
    m_data = new char[4096];
    connect(&m_serialPort,&QSerialPort::readyRead,this,&SerialClient::readyRead);
    connect(&m_serialPort,static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
            this,&SerialClient::error);
}

QStringList SerialClient::getAllSerialPort()
{
    QStringList resList;
    auto serialList = QSerialPortInfo::availablePorts();
    for(auto item : serialList){
        resList.append(item.portName());
    }
    return resList;
}

bool SerialClient::openSerialPort(QString portName, int rate, int datalen, int checklen, int stoplen)
{
    if(m_serialPort.isOpen()){
    }else{
        m_portName  = portName;
        m_rate      = rate;
        m_datalen   = datalen;
        m_checklen  = checklen;
        m_stoplen   = stoplen;
        m_serialPort.setPortName(m_portName);
        if(!m_serialPort.open(QIODevice::ReadWrite)){
            qDebug()<<"connect error";
            return false;
        }
        //波特率
        m_serialPort.setBaudRate(rate);
        //数据位
        m_serialPort.setDataBits(QSerialPort::DataBits(datalen));
        //校验位
        m_serialPort.setParity(QSerialPort::Parity(checklen));
        //停止位
        m_serialPort.setStopBits(QSerialPort::StopBits(stoplen));

        return true;
    }

}

void SerialClient::disConnectSerialPort()
{
    m_serialPort.clear();
    m_serialPort.close();
}

void SerialClient::sendData(QByteArray data)
{
    m_serialPort.write(data);
    m_serialPort.waitForBytesWritten();
}

void SerialClient::readyRead()
{

    memset(m_data,0,4096);
    int size;
    if(m_size != 0){
        size = m_serialPort.read(m_data,m_size - m_cache.size());
    }else{
        size = m_serialPort.read(m_data,4096);
    }

    m_cache.append(m_data,size);
    QByteArray arr;
    if(m_cache.size() > 7){
        m_orderName = QString::fromLatin1(m_cache.data(),5);
        arr.append(m_cache.at(6));
        m_size = arr.toHex().toInt(nullptr,16);
    }
    if(m_cache.size() >= m_size && m_size != 0 ){
        qDebug()<<__FUNCTION__<<m_cache.toHex();
        if(m_orderName == "$GLZK"){
            auto info = m_praser.parseGLJC(m_cache);
            signPrase(info);
        }else if(m_orderName == "$GPSX"){
            auto info = m_praser.parseGPS(m_cache);
            qDebug()<<"time = "<<info.time;
            signPrase(info);
        }else if(m_orderName == "$FKXX"){
            //auto info = m_praser.parseGPS(m_cache);
            qDebug()<<"rec = "<<m_cache.toHex();
            //signPrase(info);
        }else if(m_orderName == "$CPBJ"){
            //auto info = m_praser.parseGPS(m_cache);
            qDebug()<<"rec = "<<m_cache.toHex();
            signCPBJPrase();
        }else if(m_orderName == "$TXXX"){
            auto info = m_praser.parseCommunication(m_cache);
            qDebug()<<"rec = "<<m_cache.toHex();
            signPrase(info);
        }

        signRecvData(m_cache,0);
        m_cache.remove(0,m_size);
        m_size = 0;
    }

}


void SerialClient::error(QSerialPort::SerialPortError serialPortError)
{
    qDebug()<<__FUNCTION__<<serialPortError;
}
